﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using QLKetNoi;
using System.Linq;
using System.Data.Linq;


namespace QLDVu.BO
{
    class DanhSachKhachHangBO
    {
        
       
        public static Table<KHACHHANG> DanhSachKhachHang()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            return dulieu.GetTable<KHACHHANG>();
            
        }
        public static KHACHHANG KhachHangCuoiCung()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            KHACHHANG s = dulieu.KHACHHANGs.OrderByDescending(hh => hh.MaKhachHang).FirstOrDefault();
            return s;

        }
        public static KHACHHANG KhachHangCuoiCung(string makhachhang)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            KHACHHANG s = dulieu.KHACHHANGs.Where(kh => kh.MaKhachHang.Contains(makhachhang)).OrderByDescending(hh => hh.MaKhachHang).FirstOrDefault();
            return s;

        }
        public static KHACHHANG DanhSachKhachHang(string makhachhang)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<KHACHHANG> khachhang = dulieu.GetTable<KHACHHANG>();
            KHACHHANG x = khachhang.Single(kh => kh.MaKhachHang.Equals(makhachhang));
            return x;

        }
        public static Table<LOAIKHACHHANG> DanhSachLoaiKhachHang()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            return dulieu.GetTable<LOAIKHACHHANG>();

        }
        public static Table<KHUVUC> DanhSachKhuVuc()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            return dulieu.GetTable<KHUVUC>();

        }
       public static void ThemKhachHang(string makhachhang,string tenkhachhang,string loaikhachhang,
           string diachi,string makhuvuc,string dienthoai,string email)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
           Table<KHACHHANG> khachhang = dulieu.GetTable<KHACHHANG>();
            KHACHHANG kh = new KHACHHANG();
            kh.TenKhachHang = tenkhachhang;
            kh.MaLoaiKhachHang = loaikhachhang;
            kh.DiaChi = diachi;
            kh.MaKhuVuc = makhuvuc;
            kh.DienThoai = dienthoai;
            kh.Email = email;
            kh.MaKhachHang = makhachhang;
            khachhang.InsertOnSubmit(kh);
            dulieu.SubmitChanges();

        }
       public static void CapNhatKhachHang(string makhachhang, string tenkhachhang,string makhuvuc,string maloaikhachhang,string dienthoai,string email,string diachi)
       {
           QLDNDataContext dulieu = new QLDNDataContext();
           Table<KHACHHANG> khachhags = dulieu.GetTable<KHACHHANG>();
           KHACHHANG pbg = khachhags.Single(pbgs => pbgs.MaKhachHang.Equals(makhachhang));
           pbg.MaKhachHang = makhachhang;
           pbg.TenKhachHang = tenkhachhang;
           pbg.MaKhuVuc = makhuvuc;
           pbg.MaLoaiKhachHang = maloaikhachhang;
           pbg.DiaChi = diachi;
           pbg.DienThoai = dienthoai;
           pbg.Email = email;
           dulieu.SubmitChanges();
       }
       public static void XoaKhachHang(string makhachhang)
       {
           QLDNDataContext dulieu = new QLDNDataContext();
           Table<KHACHHANG> khachhags = dulieu.GetTable<KHACHHANG>();
           KHACHHANG pbg = khachhags.Single(pbgs => pbgs.MaKhachHang.Equals(makhachhang));
           khachhags.DeleteOnSubmit(pbg);
           dulieu.SubmitChanges();
       }
    }
}
